//
//  BaseTableViewController.m
//  KeleAppProject
//
//  Created by 周岑 on 2018/1/14.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "BaseTableViewController.h"


NSString * const GET_PAGE_NEW = @"new";
NSString * const GET_PAGE_NEXT = @"next";

@interface BaseTableViewController ()<UITableViewDelegate, UITableViewDataSource>



@property (nonatomic, strong) UIView *lineView;
@end

@implementation BaseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

- (void)loadTableViewWithFrame:(CGRect)frame delegate:(id)delegate{
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:frame style:UITableViewStylePlain];
    self.tableView = tableView;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    tableView.delegate = delegate;
    tableView.dataSource = delegate;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = KLColor_B3;
    [self.view addSubview:lineView];
    self.lineView = lineView;
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(1);
    }];
}

// 获取数据
- (void)getDataSourceWithPage:(NSString *)page start:(NSInteger)start{
   
}
// 进行下拉上啦
- (void)refreshListData:(NSMutableArray *)dataArr flag:(NSString *)flag{
    if ([flag isEqualToString:GET_PAGE_NEW]) {
        // 如果是下拉
        if (self.sourceData.count > 0) {
            [self.sourceData removeAllObjects];
        }
        [self.sourceData addObjectsFromArray:dataArr];
        [self.tableView reloadData];
        // 结束刷新
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    }else if ([flag isEqualToString:GET_PAGE_NEXT]){
        // 如果是上啦
        if (dataArr != nil) {
            if (dataArr.count!=0) {
                [self.sourceData addObjectsFromArray:dataArr];
                [self.tableView reloadData];
                [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.sourceData.count - PageNum inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:YES];
                // 结束刷新
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
            }else{
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }else{
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }
        
    }
    
}


// 上啦刷新  下拉加载

- (void)upLoadMore{
    __weak UITableView *tableView = self.tableView;
    
    tableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.pageNum = _pageNum + 1;
        [self getDataSourceWithPage:GET_PAGE_NEXT start:_pageNum];
    }];
}

- (void)downRefresh{
    __weak UITableView *tableView = self.tableView;
    
    tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageNum = 1;
        [self getDataSourceWithPage:GET_PAGE_NEW start:_pageNum];
    }];
}



#pragma mark --- 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.sourceData.count;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 0;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

// lazyout
- (NSMutableArray *)sourceData{
    if (!_sourceData) {
        _sourceData = [NSMutableArray array];
    }
    return _sourceData;
}


- (void)setIsShowLine:(BOOL)isShowLine{
    self.lineView.hidden = isShowLine;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
